package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.ChronicdisDao;
import com.gxuwz.medical.domain.chronicPay.ChronicPay;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.policy.Policy;
/**
 * 报销
 * @author 沉羽墨玖
 *
 */
public class ChronicPayServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m=req.getParameter("m");
	    if("list".equals(m)){
	      process(req, resp, "/page/chronicpay/paySearch_list.jsp");
	    }else if("list2".equals(m)){
			  process(req, resp, "/page/chronicpay/chronicPay_list.jsp");
		}else if("input".equals(m)){
		  process(req, resp, "/page/chronicpay/chronicPay_add.jsp");
		}else if("add".equals(m)){
			add(req, resp);
		}else if("del".equals(m)){
			del(req, resp);
		}else if("showlist".equals(m)){
			process(req, resp, "/page/chronicpay/chronicPay_showlist.jsp");
		}
		
		else{
			error(req,resp);
		}
	
	}
	/**
	 * 添加报销记录
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
		//接收页面传递过来的参数
  	  String persName=req.getParameter("persName");
  	  String cardId=req.getParameter("cardId");
  	  String illName=req.getParameter("illName");
  	  String payTime=req.getParameter("payTime");
  	  double money=Double.parseDouble(req.getParameter("money"));
		//获取当年年份
		Calendar date = Calendar.getInstance();
		String year = String.valueOf(date.get(Calendar.YEAR)); 	  
  	  try{
  		 //实例化慢性病政策对象
  		Policy policy = new Policy(year);
  	     //获取封顶线
		double maxline = policy.getMaxline(); 
		//获取报销比例
		double ratio = policy.getRatio();     
		//实例化慢性病报销对象，并设置字段的值
		ChronicPay chronicPay = new ChronicPay();
		//chronicPay.setPayID(payID);
		chronicPay.setPersName(persName);
		chronicPay.setCardId(cardId);
		chronicPay.setIllName(illName);		
		chronicPay.setYear(year);
		chronicPay.setPayTime(payTime);
		//获取已报销金额
		double sum = chronicPay.countPayMoney(year,cardId); 
		 //判断已报销金额是否超过当年的封顶线
		if(maxline -sum >0){  
			 //判断已报销金额+本次报销金额是否超过封顶线
			if(maxline-(sum+money*ratio)>=0){   
				//计算本次报销金额=住院费用*报销比例
				double payMoney = money*ratio;   
				chronicPay.setMoney(money);						
				chronicPay.setPayMoney(payMoney);
				chronicPay.add();
				process(req, resp, "/page/chronicpay/chronicPay_list.jsp");
			}else {
				//如果已报销金额+本次报销金额超过封顶线,本次报销金额=封顶线-已报销金额
				double payMoney = maxline -sum;				
				chronicPay.setMoney(money);						
				chronicPay.setPayMoney(payMoney);
				chronicPay.add();				
			}
		}else{  //如果已报销金额超过了当年封顶线，输出提示
			System.out.println("该参合农民今年报销金额已达上限！");
			 resp.setContentType("text/html;charset=utf-8");				 
			 PrintWriter out=resp.getWriter();				  			
			 out.print("<script>alert('该参合农民今年报销金额已达上限！');window.href='/page/refund/refund_edit.jsp'</script>");
		}
  		
  		
  	  }catch(Exception e){
  		e.printStackTrace();
  	  }
	}
	
	private void del(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
	  	  String payID=req.getParameter("payID");
	  	  ChronicPay chronicPay=new ChronicPay();
	  	  try{
	  		chronicPay.del(payID);
	  		process(req, resp, "/page/chronicpay/chronicPay_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
	  
		}

}
